package sifcom.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sifcom.Entidades.Factory.ProvinciaFactory;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.repackaged.org.json.JSONException;

public class ProvinciaServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -491706362764982668L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp){
		
		if(req.getParameter("provincia") ==null)
		{
		ProvinciaFactory data = new ProvinciaFactory();
		String datos = data.getData();
		
		try {
			resp.setContentType("text/html; charset=UTF-8");
			resp.getWriter().write(datos);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		else
		{
			
			ProvinciaFactory data = new ProvinciaFactory();
			String datos = data.Search(req.getParameter("provincia") != null ? req.getParameter("provincia").toString() : "");

			try {
				resp.setContentType("text/html; charset=UTF-8");
				resp.getWriter().write(datos);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp){
		try {
			new ProvinciaFactory(req.getReader());	

			this.doGet(req, resp);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
}
